package validation;

import java.util.Calendar;

public class Validate {
    /**This method checks the password strength of the user
     * according to the type of the characters entered where
     * number,alphabet & special characters are used one
     * of these make weak password and tow of them make moderate one &
     * the all of them makes strong password
     * @param str Passowrd to check its strength
     * @param min Minimum number of characters of the password
     * @return o if weak ,1 if moderate& 2 if strong
     */
        public static int passStren(String str,int min)
        {
                boolean words=false, numb=false, specialChar=false;
                      if (str.length() >= min) {
            if (str.matches("(.*)\\d+(.*)")) {
                numb = true;
            }
            if (str.matches("(.*)[a-z]+(.*)")) {
                words = true;
            }
            if (str.matches("(.*)\\W+(.*)")) {
                specialChar = true;
            }
            if (words && numb && specialChar) {
                return 2;
            }
            if ((!words && numb && specialChar) || (words && !numb && specialChar) || (words && numb && !specialChar)) {
                return 1;
            }
        }
                return 0;
        }
    /**Checks the validity of day of the month
         * @param day Day number starting from 1
         * @param month Month number starting from 1
         * @param year Year number starting from 1
         * @return True if the date is valid and false if not
         */
    public boolean dateValidity(int day, int month, int year) {
        if ((month == 1) || (month == 3) || (month == 5) || (month == 7) || (month == 8) || (month == 10) || (month == 12)) {
            if (day > 31) {
                return false;
            } else {
                Calendar date = Calendar.getInstance();
                date.set(year, month, day);
                if (date.after(Calendar.getInstance())) {
                    return false;
                } else {
                    return true;
                }
            }
        } else if ((month == 4) || (month == 6) || (month == 9) || (month == 11)) {
            if (day > 30) {
                return false;
            } else {
                Calendar date = Calendar.getInstance();
                date.set(year, month, day);
                if (date.after(Calendar.getInstance())) {
                    return false;
                } else {
                    return true;
                }
            }
        } else if (month == 2) {
            if (day > 29) {
                return false;
            } else {
                Calendar date = Calendar.getInstance();
                date.set(year, month, day);
                if (date.after(Calendar.getInstance())) {
                    return false;
                } else {
                    return true;
                }
            }
        } else {
            return false;
        }
        }
    public static boolean isLeapYear(int year)
    {
        if ((year%4==0&&year%100!=0)||(year%400==0))
                return true;
        else
            return false;
    }
    /**This method validates ID of the user if tit has special character
     * or it's too short
     *
     * @param str
     * @return
     */
    public static int validateId(String str,int minLength){
        if (str.matches(".*\\W.*"))
            return -1;
        if (str.length()<minLength)
            return -2;
        return 0;
    }
    public static int validateName(String name,int minLength){
        if (name.matches(".*\\W.*")||name.matches(".*(\\d.*){2,}.*"))
            return -1;
        if (name.length()<minLength)
            return -2;
        return 0;
    }
     public static int validateSecQn(String name,int minLength){
        if (name.matches(".*\\W.*"))
            return -1;
        if (name.length()<minLength)
            return -2;
        return 0;
    }
    public static int validateCity(String name,int minLength){
        if (name.matches(".*\\W.*"))
            return -1;
        if (name.length()<minLength)
            return -2;
        return 0;
    }
    public static int validateCountry(String name,int minLength){
        if (name.matches(".*\\W.*"))
            return -1;
        if (name.length()<minLength)
            return -2;
        return 0;
    }
     public static int validatePostalCode(String name,int minLength){
        if (name.matches(".*\\W.*"))
            return -1;
        if (name.length()<minLength)
            return -2;
        return 0;
    }
     public static int validateSecAns(String secAns,int minLength){
        if (secAns.matches(".*\\W.*"))
            return -1;
        if (secAns.length()<minLength)
            return -2;
        return 0;
    }
     public static int validateSecImage(String secImage,String imageCharacters){
         if(secImage.trim().equalsIgnoreCase(imageCharacters))
             return 0;
         return -1;
     }
     public static int validateGender(String gender){
         if(gender==null)
             return -1;
         return 0;
     }
    public static int validateEmail(String email) {
        if (!email.matches("(.+)@\\w+\\.(\\w+)")) {
            return -1;
        }
        return 0;
    }
    public static int confirmPassword(String password,String passConfirm,int minLength){
        int status=0;
        if (!password.equals(passConfirm))
            status+=-1;
        if (password.length()<minLength)
            status+=-1;
        return status;
    }
    public static String[] dayList(int itemIndex,int yearIndex) {
        if (itemIndex == 0 || itemIndex == 2 || itemIndex == 4 || itemIndex == 6 || itemIndex == 7 || itemIndex == 9 || itemIndex == 11) {
            String[] dayList = new String[31];
            for (int i = 1; i <= 31; i++) {
                dayList[i - 1] = i + "";
            }
            return dayList;
        } else if (itemIndex == 3 || itemIndex == 5 || itemIndex == 8 || itemIndex == 10) {
            String[] dayList = new String[30];
            for (int i = 1; i <= 30; i++) {
                dayList[i - 1] = i + "";
            }
            return dayList;
        } else {
            if (Validate.isLeapYear(Calendar.getInstance().get(Calendar.YEAR) - yearIndex)) {
                String[] dayList = new String[29];
                for (int i = 1; i <= 29; i++) {
                    dayList[i - 1] = i + "";
                }
                return dayList;
            } else {
                String[] dayList = new String[28];
                for (int i = 1; i <= 28; i++) {
                    dayList[i - 1] = i + "";
                }
                return dayList;
            }
        }
    }
}
